package com.open.demo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.Crossfade
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color.Companion.Blue
import androidx.compose.ui.graphics.Color.Companion.Green
import androidx.compose.ui.graphics.Color.Companion.Red
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.open.demo.bean.Data
import com.open.demo.compose.JetpackComposeActivity
import com.open.demo.flow.FlowActivity
import com.open.demo.location.GaoDeActivity
import com.open.demo.location.GmsActivity
import com.open.demo.location.GmsLocationActivity
import com.open.demo.location.GpsActivity
import com.open.demo.location.TencentLocationActivity
import com.open.demo.span.SpanActivity
import com.open.demo.协程.CoroutineTestActivity
import com.tencent.map.geolocation.TencentGeofence
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.util.HashMap

@ExperimentalComposeUiApi
class MainActivity : AppCompatActivity() {

    private val dataList by lazy {
        listOf(
            Data(JetpackComposeActivity.TAG, Intent(this, JetpackComposeActivity::class.java)),
            Data("Span", Intent(this, SpanActivity::class.java)),

            Data(CoroutineTestActivity.TAG, Intent(this, CoroutineTestActivity::class.java)),
            Data(FlowActivity.TAG, Intent(this, FlowActivity::class.java)),

            // 定位服务
            Data(GpsActivity.TAG, Intent(this, GpsActivity::class.java)),
            Data(GmsActivity.TAG, Intent(this, GmsActivity::class.java)),
            Data(GmsLocationActivity.TAG, Intent(this, GmsLocationActivity::class.java)),
            // 高德定位服务
            Data(GaoDeActivity.TAG, Intent(this, GaoDeActivity::class.java)),
            // 腾讯定位服务 Demo
            Data(TencentLocationActivity.TAG, Intent(this, TencentLocationActivity::class.java))
        )
    }

    companion object {
        const val TAG = "hailong.qiu"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            var color by remember { mutableStateOf(Blue) }
            // 列表: https://developer.android.com/jetpack/compose/lists
            LazyColumn(
                modifier = Modifier.fillMaxWidth(),
                contentPadding= PaddingValues(vertical = 30.dp),
                verticalArrangement= Arrangement.spacedBy(10.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                dataList.forEach { data ->
                    item {
                        val click = { startActivity(data.intent) }
                        Button(
                            modifier = Modifier
                                .border(3.dp, color)
                                .focusTarget()
                                .focusable()
                                .onFocusChanged {
                                    color = if (it.isFocused) Red else Blue
                                }
                                .onKeyEvent { keyEvent ->
                                    if (Key.DirectionCenter == keyEvent.key) {
                                        click()
                                        Log.d(TAG, "data.title onKeyEvent keyEvent:${keyEvent.key}")
                                    }
                                    false
                                },
                            shape = RoundedCornerShape(50),
                            onClick = click,
                            //                            colors = ButtonDefaults.buttonColors(backgroundColor = ButtonState())
                        ) {
                            Text(text = data.title)
                        }
                    }
                }
                item {
                    Row() {
                        val focusManager = LocalFocusManager.current
                        Text(text = "hailong.qiu")
                        Button(onClick = { }) {
                            Text(
                                text = "测试"
                            )
                        }
                        Button(onClick = { /*TODO*/ }) {
                            Text(text = "测试")
                        }
                        Button(onClick = { /*TODO*/ }) {
                            Text(text = "测试")
                        }
                        Button(onClick = { /*TODO*/ }) {
                            Text(text = "测试")
                        }
                        Button(onClick = { /*TODO*/ }) {
                            Text(text = "测试")
                        }
                    }

                    val (first, second, b, d) = FocusRequester.createRefs()
                    Column(modifier = Modifier
                        .background(Color.Blue)
                        .animateContentSize()
                        .focusTarget()
                        .onFocusChanged { focusState ->
                            Log.d(TAG, "Column onFocusChanged focusState isFocused:${focusState.isFocused} hasFocus:${focusState.hasFocus}")
                        }
                        .onFocusEvent { focusState ->
                            Log.d(TAG, "Column onFocusEvent focusState isFocused:${focusState.isFocused} hasFocus:${focusState.hasFocus}")
                        }) {

                        val focusManager = LocalFocusManager.current
                        val requester = FocusRequester()
                        Button(
                            onClick = { Toast.makeText(this@MainActivity, "按钮点击测试", Toast.LENGTH_LONG).show() },
                        ) {
                            Text(text = "button1")
                        }
                        Button(
                            onClick = { Toast.makeText(this@MainActivity, "按钮点击测试", Toast.LENGTH_LONG).show() },
                        ) {
                            Text(text = "button2")
                        }
                        //                        TextField(value = "文本输入1", onValueChange = {},
                        //                            keyboardActions = KeyboardActions(
                        //                                onNext = {
                        //                                    focusManager.moveFocus(FocusDirection.Down)
                        //                                }
                        //                            ),
                        //                        )
                        //                        TextField(
                        //                            value = "文本输入2", onValueChange = {},
                        //                            keyboardActions = KeyboardActions(
                        //                                onNext = {
                        //                                    focusManager.moveFocus(FocusDirection.Up)
                        //                                }
                        //                            ),
                        //                            //                            modifier = Modifier.focusRequester(requester)
                        //                            //                            modifier = Modifier.focusOrder(first)
                        //                            //                        modifier = Modifier.focusTarget()
                        ////                            modifier = Modifier.onFocusEvent {
                        ////                            }
                        ////                            modifier = Modifier.onFocusChanged {
                        ////                            }
                        //                        )
                    }
                }
            }
        }
    }

    fun test() {
        // 淡入淡出动画再两个布局之间添加动画效果
//        Crossfade(targetState = ) {
//
//        }
    }
}
